<style lang="less">
@import './main.less';
</style>

<template>
    <div class="main" :class="{'main-hide-text': shrink}">
      <div class="sidebar-menu-con" :style="{width: shrink?'60px':'200px', overflow: shrink ? 'visible' : 'auto'}">
          <shrinkable-menu :shrink="shrink" :menu-list="menuList">
            <h1 slot="top" class="logo-con">
              <span v-show="shrink">课</span>
              <span v-show="!shrink">壹键通微课管理后台</span>
            </h1>
          </shrinkable-menu>
      </div>
      <div class="main-header-con" :style="{paddingLeft: shrink?'60px':'200px'}">
        <div class="main-header">
            <div class="navicon-con">
                <i-button :style="{transform: 'rotateZ(' + (this.shrink ? '-90' : '0') + 'deg)'}" type="text" @click="toggleClick">
                    <i-icon type="navicon" size="32"></i-icon>
                </i-button>
                <i-breadcrumb class="main-breadcrumb">
                    <i-breadcrumb-item :to="{path: '/admin/index'}">管理首页</i-breadcrumb-item>
                </i-breadcrumb>
            </div>
            <div class="header-avator-con">
                <div class="user-dropdown-menu-con">
                    <i-row type="flex" justify="end" align="middle" class="user-dropdown-innercon">
                        <i-dropdown transfer trigger="click" @on-click="handleClickUserDropdown">
                            <a href="javascript:void(0)">
                                <span class="main-user-name">{{ userName }}</span>
                                <i-icon type="arrow-down-b"></i-icon>
                            </a>
                            <i-dropdown-menu slot="list">
                              <i-dropdown-item><router-link :to="{name: 'home'}" target="_blank">打开首页</router-link></i-dropdown-item>
                              <i-dropdown-item name="logout" divided>退出登录</i-dropdown-item>
                            </i-dropdown-menu>
                        </i-dropdown>
                        <i-avatar icon="person" style="background: #619fe7;margin-left:10px;"></i-avatar>
                    </i-row>
                </div>
            </div>
        </div>
    </div>
    <div class="single-page-con" :style="{left: shrink?'60px':'200px'}">
      <div class="single-page">
        <keep-alive :include="cachePage">
            <router-view></router-view>
        </keep-alive>
      </div>
    </div>
  </div>
</template>

<script>
import util from '@/libs/util.js'
import { mapGetters, mapActions } from 'vuex'
import shrinkableMenu from './components/shrinkable-menu/shrinkable-menu.vue'

export default {
  components: {
    shrinkableMenu
  },
  data() {
    return {
      shrink: false,
      version: window.__version
    }
  },
  computed: {
    ...mapGetters({
      account: 'account'
    }),
    userName() {
      return this.account.name
    },
    cachePage() {
      return this.$store.state.app.cachePage
    },
    menuList() {
      return this.$store.state.app.menuList
    },
    currentPath() {
      return this.$store.state.app.currentPath // 当前面包屑数组
    }
  },
  methods: {
    ...mapActions({
      getLoggedInfo: 'getLoggedInfo'
    }),
    toggleClick() {
      this.shrink = !this.shrink
    },
    handleClickUserDropdown(name) {
      if (name !== 'logout') return
      this.$router.push({
        name: 'logout'
      })
    }
  },
  watch: {
    lang() {
      util.setCurrentPath(this, this.$route.name) // 在切换语言时用于刷新面包屑
    }
  },
  async activated() {
    await this.getLoggedInfo()
    if (!this.account.isAdmin) {
      this.$router.push('/')
      return
    }
    this.$store.commit('updateMenulist')
  }
}
</script>
